package com.sugar.queryinfo.module.business.api.bi;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.sugar.queryinfo.framework.common.exception.ServerException;
import com.sugar.queryinfo.framework.common.exception.ServiceException;
import com.sugar.queryinfo.module.business.api.bi.dto.req.common.UserTwoInfoReqVO;
import com.sugar.queryinfo.module.business.api.bi.dto.req.common.UserThreeInfoReqVO;
import com.sugar.queryinfo.module.business.api.bi.dto.req.common.ZCI101ReqVO;
import com.sugar.queryinfo.module.business.api.bi.dto.req.sjzwsj.ZCI003ReqVO;
import com.sugar.queryinfo.module.business.api.bi.dto.req.sjzwsj.ZCI040ReqVO;
import com.sugar.queryinfo.module.business.api.bi.dto.resp.*;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.sugar.queryinfo.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.sugar.queryinfo.module.business.api.bi.BackInvestApiEnum.*;
import static com.sugar.queryinfo.module.business.enums.ErrorCodeConstants.BACK_INVEST_API_INVOKE_EXCEPTION;
import static com.sugar.queryinfo.module.business.enums.ErrorCodeConstants.BACK_INVEST_API_INVOKE_FAIL;

@Service
public class BackInvestApiImpl implements BackInvestApi {

    @Override
    public ZCI001RespVO authTwoE(UserTwoInfoReqVO reqVO) {
        return doApi(SF_2E_AUTH, reqVO, ZCI001RespVO.class);
    }

    @Override
    public ZCI002RespVO authThreeE(UserThreeInfoReqVO reqVO) {
        return doApi(SF_3E_AUTH, reqVO, ZCI002RespVO.class);
    }

    @Override
    public ZCI003RespVO sjzwsc(ZCI003ReqVO reqVO) {
        return doApi(SJZWSC, reqVO, ZCI003RespVO.class);
    }

    @Override
    public List<ZCI007RespVO> fyfx(UserTwoInfoReqVO reqVO) {
        return doApiList(FYFX, reqVO, ZCI007RespVO.class);
    }

    @Override
    public ZCI008RespVO qjld(UserThreeInfoReqVO reqVO) {
        return doApi(QJLD_V4, reqVO, ZCI008RespVO.class);
    }

    @Override
    public ZCI021RespVO jxzs(UserThreeInfoReqVO reqVO) {
        return doApi(JXZS, reqVO, ZCI021RespVO.class);
    }

    @Override
    public ZCI022RespVO qjdan(UserThreeInfoReqVO reqVO) {
        return doApi(QJ_DAN, reqVO, ZCI022RespVO.class);
    }

    @Override
    public ZCI023RespVO tzC(UserThreeInfoReqVO reqVO) {
        return doApi(TZ_C, reqVO, ZCI023RespVO.class);
    }

    @Override
    public ZCI034RespVO xfjytz(UserThreeInfoReqVO reqVO) {
        return doApi(XFJYTZ, reqVO, ZCI034RespVO.class);
    }

    @Override
    public String lsbg(String idCard) {
        try {
            String resultStr = HttpUtil.get("https://www.zhichajinkong.com/dataMiddle/api/getFlow?idCard=" + idCard);
            LOG.info("调用【流水报告】接口返回：{}", resultStr);
            JSONObject resultJson = JSONUtil.parseObj(resultStr);
            Integer code = resultJson.getInt("code");
            if (code == null || code != 200)
                throw exception(BACK_INVEST_API_INVOKE_EXCEPTION, resultJson.getStr("message"));
            else {
                String data = resultJson.getStr("data");
                if (StrUtil.isBlank(data))
                    throw exception(BACK_INVEST_API_INVOKE_EXCEPTION, resultJson.getStr("message"));

                return data;
            }
        }catch (ServiceException se){
            throw se;
        }catch (Throwable e){
            LOG.error("流水报告查询失败：", e);
            throw exception(BACK_INVEST_API_INVOKE_FAIL);
        }
    }

    @Override
    public ZCI040RespVO znbd(ZCI040ReqVO reqVO) {
        return doApi(ZNBD, reqVO, ZCI040RespVO.class);
    }

    @Override
    public ZCI101RespVO sfbg(ZCI101ReqVO reqVO) {
        return doApi(WDGZ, reqVO, ZCI101RespVO.class);
    }

    @Override
    public ZCI105RespVO yqtzbg(ZCI101ReqVO reqVO) {
        return doApi(FQZ, reqVO, ZCI105RespVO.class);
    }

    @Override
    public ZCI104RespVO zcqjbg(ZCI101ReqVO reqVO) {
        return doApi(XFHX, reqVO, ZCI104RespVO.class);
    }

    @Override
    public ZCI106RespVO tcbg(ZCI101ReqVO reqVO) {
        return doApi(DTJD, reqVO, ZCI106RespVO.class);
    }
}
